import { Component, OnInit } from '@angular/core';
import { GetdataService } from '../../service/getdata/getdata.service';
import { CommondataService } from '../../service/commondata/commondata.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Router } from '@angular/router';
@Component({
  selector: 'app-searchpage',
  templateUrl: './searchpage.component.html',
  styleUrls: ['./searchpage.component.less']
})
export class SearchpageComponent implements OnInit {

  areaNumber: any = [];    // 初始化主页面的下拉列表框的内容
  modelUserCarPort: any;    // 用户车位数据记录
  modelIdealArea: any;      // 用户选择的理想车位数据记录
  modelSuitableArea: any = [];   // 用户选择的合适车位数据记录
  song: any = {
    userName: '',
    userCarportArea: '',
    idealArea: '',
    contactInformation: '',
    suitableArea: ''
  };
  tipNumber: any[] = [];
  showTipNumber: any[] = [];
  str: any = '';
  constructor(private message: NzMessageService,
    private dataService: GetdataService,
    private router: Router,
    private commonData: CommondataService) { }
  // 理想车位区域的选项改变将对应的附近车位区域在次选车位下拉框中有所提示
  messageChange() {
    this.showTipNumber = this.tipNumber.filter((d => d[this.modelIdealArea]))[0][this.modelIdealArea];
  }

  ngOnInit() {

    this.dataService.getMessage().subscribe(data => {
      data.map((d: any) => {
        this.areaNumber.push(d.area_mark);
        const list: any[] = [];
        let i = 0;
        const len = Object.keys(d).length;
        Object.keys(d).forEach((key) => {
          if (i >= 3 && i < len - 1) {
            if (d[key] != null) {
              list.push(d[key]);
            }
          }
          i++;
        });
        const obj = new Object();
        obj[d.area_mark] = list;
        this.tipNumber.push(obj);
      });
    });
  }

  isVisible: boolean = false;     // 用户点击重置按钮信息提示框的显隐标志
  // 点击重置按钮显示信息提示框
  showModal() {
    this.isVisible = true;
  }
  // 用户点击信息提示框的取消按钮则取消提示框
  handleCancel() {
    this.isVisible = false;
  }
  // 用户点击信息提示框的确认按钮则将所有输入的数据清空
  handleOk() {
    this.isVisible = false;
    this.modelIdealArea = undefined;
    this.modelSuitableArea = undefined;
    this.modelUserCarPort = undefined;
  }

  // 数据提交触发事件，并且根据传输的数据返回查询结果
  submitForm(): void {
    // 判断是否满足查询条件否则不予通过
    if (this.modelUserCarPort == null || this.modelIdealArea == null || this.modelSuitableArea.length === 0) {
      this.message.create('warning', `数据不能为空哟~`);
      return;
    }

    // 如果用户选择了合适的车位区域，将合适车位区域信息用“+”连接成字符串
    if (this.modelSuitableArea != null) {
      for (let k = 0; k < this.modelSuitableArea.length; k++) {
        if (k === this.modelSuitableArea.length - 1) {
          this.str += this.modelSuitableArea[k];
        } else {
          this.str += this.modelSuitableArea[k] + '+';
        }
      }
    }
    this.song.userCarportArea = this.modelUserCarPort;    // 为用户车位信息赋值
    this.song.idealArea = this.modelIdealArea;            // 为用户选择的理想车位区域赋值
    this.song.suitableArea = this.str;                    // 将处理的合适车位区域的字符串赋值给对象的值
    this.dataService.postCar(this.song).subscribe((data) => {     // 发送请求
      this.commonData.setData(data, this.song);      // 将取得的数据提交给服务进行保存
      this.router.navigateByUrl('/message');  // 路由跳转到数据结果展示界面
      this.str = '';    // 处理完毕后将处理合适车位的字符串重置
    });

  }
}
